<template>
	<view class="wrap">
		<!-- 当前手机号 -->
		<view class="current-phone">
			<view class="title">当前绑定手机号</view>
			<view class="phone">{{formatPhone(currentPhone)}}</view>
		</view>

		<!-- 更换按钮 -->
		<view class="change-btn" @tap="showChangePhone">
			更换手机号
		</view>

		<!-- 更换手机号弹窗 -->
		<u-popup v-model="showBindPhone" mode="center" :round="10">
			<view class="phone-popup">
				<view class="popup-header">
					<text>更换手机号</text>
					<view class="close-btn" @tap="showBindPhone = false">
						<u-icon name="close" color="#999" size="32"></u-icon>
					</view>
				</view>
				<view class="popup-content">
					<view class="input-item">
						<view class="prefix">+86</view>
						<u-input 
							v-model="bindPhoneForm.phone" 
							type="number" 
							:maxlength="11"
							placeholder="请输入新绑定手机号"
							border="none"
						/>
					</view>
					<view class="input-item">
						<u-input 
							v-model="bindPhoneForm.code" 
							type="number" 
							:maxlength="6"
							placeholder="请输入验证码"
							border="none"
						/>
						<view class="code-btn" @tap="getCode" :class="{'disabled': counting}">
							{{counting ? `${counter}s` : '获取验证码'}}
						</view>
					</view>
					<view class="confirm-btn" @tap="confirmChange">
						立即绑定
					</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				currentPhone: '',
				showBindPhone: false,
				bindPhoneForm: {
					phone: '',
					code: ''
				},
				counting: false,
				counter: 60
			}
		},
		onLoad() {
			// 获取当前用户手机号
			const userInfo = uni.getStorageSync('userInfo');
			if (userInfo) {
				this.currentPhone = JSON.parse(userInfo).phone || '';
			}
		},
		methods: {
			// 格式化手机号
			formatPhone(phone) {
				if (!phone) return '未绑定';
				return phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2');
			},
			// 显示更换手机号弹窗
			showChangePhone() {
				this.showBindPhone = true;
				this.bindPhoneForm.phone = '';
				this.bindPhoneForm.code = '';
			},
			// 获取验证码
			getCode() {
				if (this.counting) return;
				if (!this.bindPhoneForm.phone) {
					uni.showToast({
						title: '请输入手机号',
						icon: 'none'
					});
					return;
				}
				if (!/^1[3456789]\d{9}$/.test(this.bindPhoneForm.phone)) {
					uni.showToast({
						title: '请输入正确的手机号',
						icon: 'none'
					});
					return;
				}
				// 开始倒计时
				this.counting = true;
				this.counter = 60;
				const timer = setInterval(() => {
					this.counter--;
					if (this.counter <= 0) {
						clearInterval(timer);
						this.counting = false;
					}
				}, 1000);

				// TODO: 调用发送验证码接口
				uni.showToast({
					title: '验证码已发送',
					icon: 'none'
				});
			},
			// 确认更换手机号
			async confirmChange() {
				if (!this.bindPhoneForm.phone || !this.bindPhoneForm.code) {
					uni.showToast({
						title: '请填写完整信息',
						icon: 'none'
					});
					return;
				}

				try {
					// TODO: 调用更换手机号接口
					// const res = await changePhone(this.bindPhoneForm);
					
					// 更新本地存储的用户信息
					const userInfo = uni.getStorageSync('userInfo');
					if (userInfo) {
						const info = JSON.parse(userInfo);
						info.phone = this.bindPhoneForm.phone;
						uni.setStorageSync('userInfo', JSON.stringify(info));
					}

					this.currentPhone = this.bindPhoneForm.phone;
					this.showBindPhone = false;
					
					uni.showToast({
						title: '更换成功',
						icon: 'success'
					});
				} catch (error) {
					uni.showToast({
						title: '更换失败',
						icon: 'none'
					});
				}
			}
		}
	}
</script>

<style lang="scss">
	.wrap {
		min-height: 100vh;
		background: #f5f5f5;
		padding: 20rpx;

		.current-phone {
			background: #fff;
			border-radius: 16rpx;
			padding: 40rpx;
			margin-bottom: 30rpx;

			.title {
				font-size: 28rpx;
				color: #999;
				margin-bottom: 20rpx;
			}

			.phone {
				font-size: 40rpx;
				color: #333;
				font-weight: bold;
			}
		}

		.change-btn {
			background: #fff;
			border-radius: 16rpx;
			padding: 30rpx;
			text-align: center;
			color: #EE2F37;
			font-size: 32rpx;
			font-weight: bold;

			&:active {
				opacity: 0.8;
			}
		}
	}

	.phone-popup {
		width: 600rpx;
		background: #fff;
		border-radius: 20rpx;
		
		.popup-header {
			position: relative;
			padding: 30rpx;
			text-align: center;
			font-size: 32rpx;
			font-weight: bold;
			border-bottom: 1rpx solid #f5f5f5;
			
			.close-btn {
				position: absolute;
				right: 20rpx;
				top: 50%;
				transform: translateY(-50%);
				padding: 10rpx;
			}
		}
		
		.popup-content {
			padding: 40rpx;
			
			.input-item {
				display: flex;
				align-items: center;
				padding: 20rpx 30rpx;
				background: #f8f8f8;
				border-radius: 40rpx;
				margin-bottom: 30rpx;
				
				.prefix {
					font-size: 32rpx;
					color: #333;
					margin-right: 20rpx;
					padding-right: 20rpx;
					border-right: 1rpx solid #ddd;
				}
				
				.code-btn {
					font-size: 28rpx;
					color: #EE2F37;
					white-space: nowrap;
					padding-left: 20rpx;
					border-left: 1rpx solid #ddd;
					
					&.disabled {
						color: #999;
					}
				}
			}
			
			.confirm-btn {
				margin-top: 40rpx;
				height: 88rpx;
				line-height: 88rpx;
				text-align: center;
				background: #EE2F37;
				color: #fff;
				font-size: 32rpx;
				font-weight: bold;
				border-radius: 44rpx;
				
				&:active {
					opacity: 0.9;
				}
			}
		}
	}
</style> 